package com.sanyang.lawyer.controller.record;

import com.sanyang.lawyer.entity.*;
import com.sanyang.lawyer.service.Case.Case_courtService;
import com.sanyang.lawyer.service.client.ClientService;
import com.sanyang.lawyer.service.hr.HrEmpService;
import com.sanyang.lawyer.service.record.RecordBorrowService;
import com.sanyang.lawyer.service.record.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: lawyer
 * @description: 归档的Controller层
 * @author: Mr.Wang
 * @create: 2019-11-08 08:19
 **/
@RestController
@RequestMapping("borrow")
public class RecordBorrowController {

    @Autowired
    private RecordBorrowService borrow;
    @Autowired
    private RecordService recordService;
    @Autowired
    private ClientService clientService;
    @Autowired
    private Case_courtService case_courtService;
    @Autowired
    private HrEmpService hrEmpService;

    @RequestMapping("listall")
    public List<Record_borrow> listall(){
        List<Record_borrow> list=borrow.listall();
        return list;
    }

    @RequestMapping("getbyid")
    public Record_borrow getbyid(int id){
       Record_borrow borrows=borrow.getbyid(id);
        return borrows;
    }

    @RequestMapping("furry")
    public Map<String,Object> furry(HttpServletRequest request,Record_borrow record_borrow){
        System.out.println("record: "+record_borrow.toString());
        Map<String,Object>map=new HashMap<String,Object>();
        String name = request.getParameter("name");
        String sther = request.getParameter("sther");
        String returntype = request.getParameter("returntype");
        Recordpage recordpage = new Recordpage(record_borrow,request);
        System.out.println("分页类："+recordpage.getPageSize()+" : "+recordpage.getPageStart());
        map.put("pagetext",recordpage);
        map.put("sther",sther);
        if(name!=""&&name!=null){
            switch (sther){
                case "1":
                    String casetext1=null;
                    casetext1=name;
                    if(casetext1!=null&&casetext1!="") {
                        map.put("casetext1", casetext1);  //案号
                    }
                    break;
                case "2":
                    String casetext2=null;
                    casetext2=name;
                    if(casetext2!=null&&casetext2!="") {
                        map.put("casetext2", casetext2);  //档案号搜索
                    }
                    break;
                case "3":
                    String casetext3=null;
                    casetext3=name;
                    if(casetext3!=null&&casetext3!=""){
                        map.put("casetext3",casetext3);  //档案名称搜索
                    }
                    break;
                case "4":
                    String casetext4=null;
                    casetext4=name;
                    if(casetext4!=null&&casetext4!=""){
                        List<Hr_emp> emp = hrEmpService.getByEmpName(casetext4);
                        if(map.size()>=1&&map.size()<1){
                            map.put("casetext4",emp.get(0).getEmpId()+"");  //借阅人查询
                        }
                    }
                    break;
            }
            switch (returntype){
                case "1":
                    map.put("returntype1","Q");  //所有归还状态
                    break;
                case "2":
                        map.put("returntype2", "N");  //未归还
                    break;
                case "3":
                        map.put("returntype3","Y");  //已归还
                    break;
            }
        }else{
            switch (returntype){
                case "1":
                    map.put("returntype1","Q");  //所有归还状态
                    break;
                case "2":
                    map.put("returntype2", "N");  //未归还
                    break;
                case "3":
                    map.put("returntype3","Y");  //已归还
                    break;
            }
        }
        List<Record_borrow> list=borrow.furry(map);
        System.out.println("map:"+map.toString());
        Integer count = borrow.listcount(map);
        Map<String,Object> maps = new HashMap<String,Object>();
        if(list.size()<=0){
            count=0;
            maps.put("code",0);
            maps.put("msg","");
            maps.put("count",count);
            maps.put("data","");
        }else{
            maps.put("code",0);
            maps.put("msg","");
            maps.put("count",count);
            maps.put("data",list);
        }
        return maps;
    }

    @RequestMapping("insertborrow")
    public String insertborrow(String borrowname,String casechoice,String record,String empname,String outoftime,String returntime,String remark){
        Record_borrow borro = new Record_borrow();

        System.out.println("record: "+record);
        Case_law case_law = recordService.getbycaselaw(casechoice);
        borro.setCaseId(case_law);
        Record record1 = recordService.getbyrecord(Integer.parseInt(record));
        borro.setBorrowName(borrowname);
        System.out.println(record1.toString());
        borro.setRecordId(record1);
        List<Hr_emp> emp = hrEmpService.getByEmpName(empname);
        if(emp.size()>=1){
            borro.setEmpId(emp.get(0));
            borro.setOutoftime(outoftime);
            borro.setReturntime(returntime);
            borro.setRemark(remark);
            borro.setReturntype("N");
            System.out.println("borrowname: "+borrowname);
            System.out.println(""+borro.toString());
            borrow.insert(borro);
            return  "ok";
        }else{
            return "no";
        }

    }
    @RequestMapping("getbyborrow")
    public Record_borrow getbyborrow(String borrowid){
        System.out.println("borrowid:"+borrowid);
        Record_borrow record_borrow = borrow.getbyid(Integer.parseInt(borrowid));
        return record_borrow;
    }

    @RequestMapping("getbycase")
    public Case_law getbycase(String caseId){
        Case_law case_law=recordService.getbycaselaw(caseId);
        return  case_law;
    }

    @RequestMapping("updatetypereturn")
    public String updatetypereturn(String borrowid){
        borrow.updatestate(Integer.parseInt(borrowid));
        return "ok";
    }
    @RequestMapping("updateborrow")
    public String updateborrow(String borrowname,String casechoice,String record,String empname,String outoftime,String returntime,String remark,String borrowid){
        Record_borrow borro = new Record_borrow();
        System.out.println("borrowname: "+borrowname);
        System.out.println("record: "+record);
        Case_law case_law = recordService.getbycaselaw(casechoice);
        borro.setCaseId(case_law);
        borro.setBorrowName(borrowname);
        Record record1 = recordService.getbyrecord(Integer.parseInt(record));
        System.out.println(record1.toString());
        borro.setRecordId(record1);
        List<Hr_emp> emp = hrEmpService.getByEmpName(empname);
        if(emp.size()>=1){
            borro.setEmpId(emp.get(0));
            borro.setOutoftime(outoftime);
            borro.setReturntime(returntime);
            borro.setRemark(remark);
            borro.setBorrowId(Integer.parseInt(borrowid));
            borro.setReturntype("N");
            System.out.println(borro.toString());
            borrow.update(borro);
            return  "ok";
        }else{
            return "no";
        }
    }
    @RequestMapping("deleteborrow")
    public String deleteborrow(String borrowid) {
        if (borrowid != "" && borrowid != null) {
            borrow.deleteborrow(Integer.parseInt(borrowid));
            return "ok";
        } else
            return "no";
        }
}
